package com.cnplan.auth.realm;

import com.cnplan.entity.SystemRole;
import com.cnplan.entity.SystemUser;
import com.cnplan.service.AuthService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2018/4/25.
 */
@Service
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private AuthService authService;

    @Override
    public String getName() {
        return "AdminRealm";
    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        //分配角色
        String username = SecurityUtils.getSubject().getPrincipal().toString();
        SystemUser user = this.authService.getSystemUserByUsername(username);
        List<SystemRole> roles =this.authService.listRoleByUserId(user.getId());
        Set<String> stringSet = new HashSet<String>();
        for(SystemRole role :roles){
            stringSet.add(role.getRoleName());
        }
        simpleAuthorizationInfo.setRoles(stringSet);
        return simpleAuthorizationInfo;
    }


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        SystemUser user = new SystemUser();
        user.setUserName(username);
        SystemUser systemUser = authService.getSystemUser(user);

        if (systemUser == null) {
            throw new UnknownAccountException();
        }
        if (!systemUser.getActive()) {
            throw new UnknownAccountException();
        }

        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                systemUser.getUserName(),
                systemUser.getUserPassword(),
                ByteSource.Util.bytes(systemUser.getPasswordSalt()),
                getName()
        );

        return simpleAuthenticationInfo;
    }

    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }
}
